package com.xbts.common.core.enums;

import java.sql.*;
import java.util.*;

/**
 * 车间类型枚举
 *
 * @author xbts
 */
public enum WorkshopTypeEnum {
    OVERALL("总体", ""),
    CUTTING("开料", "开料"),
    DRILL("钻孔", "钻孔"),
    PLATING("电镀", "图形电镀,沉铜"),
    LINE("线路", "线路制作,线路"),
    LINE_DETECTION("线路检测", "AOI"),
    SOLDER_MASK("防焊", "防焊"),
    CHARACTERS("文字", "文字"),
    FORMING("成型", "成型"),
    ELECTRICAL_MEASUREMENT("电测", "电测"),
    FINAL_INSPECTION("终检", "FQC"),
    PACKAGING("包装", "包装"),
    SPRAY("喷锡", "喷锡"),
    HUAJIN("化金", "化金"),
    ;
    private final String value;
    private String erpValue;
    WorkshopTypeEnum(String value, String erpValue) {
        this.value = value;
        this.erpValue = erpValue;
    }

    public String getValue() {
        return value;
    }
    public String getErpValue() {
        return erpValue;
    }

    public static void setErpValue(String value, String erpValue) {
        for (WorkshopTypeEnum workshopType : WorkshopTypeEnum.values()) {
            if (workshopType.value.equals(value)) {
                workshopType.erpValue = erpValue;
                break;
            }
        }
    }
    public static List<String> getNameList() {
        List<String> list = new ArrayList<>();
        for (WorkshopTypeEnum value : WorkshopTypeEnum.values()) {
            list.add(value.name());
        }
        return list;
    }
    public static List<String> getValueList() {
        List<String> list = new ArrayList<>();
        for (WorkshopTypeEnum value : WorkshopTypeEnum.values()) {
            list.add(value.getValue());
        }
        return list;
    }
    public static List<String> getErpValueList() {
        List<String> list = new ArrayList<>();
        for (WorkshopTypeEnum value : WorkshopTypeEnum.values()) {
            list.add(value.getErpValue());
        }
        return list;
    }
    public static String getErpValueByValue(String value) {
        for (WorkshopTypeEnum item : WorkshopTypeEnum.values()) {
            if (item.getValue().equals(value)) {
                return item.erpValue == null ? "" : item.erpValue;
            }
        }
        return null;
    }

    public static void initializeFromDatabase(String jdbcUrl, String jdbcUser, String jdbcPassword) {

        // 数据库连接和查询
        try (Connection connection = DriverManager.getConnection(jdbcUrl, jdbcUser, jdbcPassword);
             Statement statement = connection.createStatement();
             ResultSet resultSet = statement.executeQuery("SELECT * FROM we_workshop_type")) {
            while (resultSet.next()) {
                WorkshopTypeEnum.setErpValue(resultSet.getString("type"), resultSet.getString("erp_value"));
            }
        } catch (SQLException e) {
            e.printStackTrace(); // 处理数据库连接错误
        }
    }
}
